import { listSpaces } from "@huggingface/hub";
import { headers } from "next/headers";
import { NextResponse } from "next/server";

export async function GET() {
  // 开发环境直接返回模拟用户数据
  if (process.env.NODE_ENV === "development") {
    const mockUser = {
      id: "dev_user_123",
      name: "Local Developer",
      username: "localdev",
      email: "dev@localhost.com",
      avatar_url: "https://via.placeholder.com/100",
      type: "user"
    };

    return NextResponse.json({ 
      user: mockUser, 
      projects: [], 
      errCode: null 
    }, { status: 200 });
  }

  const authHeaders = await headers();
  const token = authHeaders.get("Authorization");
  if (!token) {
    return NextResponse.json({ user: null, errCode: 401 }, { status: 401 });
  }

  const userResponse = await fetch("https://huggingface.co/api/whoami-v2", {
    headers: {
      Authorization: `${token}`,
    },
  });

  if (!userResponse.ok) {
    return NextResponse.json(
      { user: null, errCode: userResponse.status },
      { status: userResponse.status }
    );
  }
  const user = await userResponse.json();
  const projects = [];
  for await (const space of listSpaces({
    accessToken: token.replace("Bearer ", "") as string,
    additionalFields: ["author", "cardData"],
    search: {
      owner: user.name,
    }
  })) {
    if (
      space.sdk === "static" &&
      Array.isArray((space.cardData as { tags?: string[] })?.tags) &&
      (
        ((space.cardData as { tags?: string[] })?.tags?.includes("deepsite-v3")) ||
        ((space.cardData as { tags?: string[] })?.tags?.includes("deepsite"))
      )
    ) {
      projects.push(space);
    }
  }

  return NextResponse.json({ user, projects, errCode: null }, { status: 200 });
}
